﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonBase;
using McReports.Common;
using Microsoft.Office.Interop.Excel;
using MCDomain.DataAccess;
using MCDomain.Model;

namespace McReports.ViewModel
{
    public class InformationConcludedContracts_ViewModel : ExcelReportViewModel
    {
        public InformationConcludedContracts_ViewModel(IContractRepository contractRepository)
            : base(contractRepository)
        {
            EndData = DateTime.Today;
        }

        [InputParameter(typeof (DateTime), "Начало периода")]
        public DateTime StartData { get; set; }

        [InputParameter(typeof(DateTime), "Конец периода")]
        public DateTime EndData { get; set; }

        public override string DisplayName
        {
            get { return "Информация о заключенных договорах"; }
            protected set { base.DisplayName = value; }
        }

        protected override string ReportFileName
        {
            get { return string.Empty; }
        }

        public override string MainWorkSheetName
        {
            get { return ActiveWorksheet.Name; }
        }

        private string NormalPrice(decimal priceValue, long factor)
        {
                string price = "";
                switch (factor)
                {
                    case 1: //в руб.
                        {

                            price = ((long)priceValue * 0.000001).ToString(NumberFormat);
                            break;
                        }
                    case 1000000:  //в млн. руб.
                        {
                            price = priceValue.ToString(NumberFormat);
                            break;
                        }
                }

                return price;
            }

        //находит договоры, утверждение которых, произошло в указанном диапазоне
        private IEnumerable<Contractdoc> SelectedContracts
        {
            get
            {
                return Repository.Contracts.Where(x => (x.Approvedat >= StartData&&x.Approvedat<=EndData));
            }
        }

        protected override void BuildReport()
        {
            
            CurrentButtomPosition = 7;
            int i = 1;
                foreach (var item in SelectedContracts)
                {
                    if (item != null)
                    {
                        MainWorkSheet.Cells[CurrentButtomPosition, 1] = i;
                        
                        MainWorkSheet.Cells[CurrentButtomPosition, 2] = item.Contractor.Inn ?? "-";   //ИНН
                        MainWorkSheet.Cells[CurrentButtomPosition, 3] = item.Contractor.Ogrn ?? "-";  //ОГРН
                        MainWorkSheet.Cells[CurrentButtomPosition, 4] = item.Contractor.Name ?? "-";  //наименование организации
                        MainWorkSheet.Cells[CurrentButtomPosition, 5] = item.Contractor.Okved ?? "-"; //ОКВЭД

                        if (item.Contractor.FamilyName != null && item.Contractor.FirstName != null && item.Contractor.MiddleName != null)
                        {
                            MainWorkSheet.Cells[CurrentButtomPosition, 6] = item.Contractor.FamilyName + " "
                                                                            + item.Contractor.FirstName + " " +
                                                                            item.Contractor.MiddleName; //ФИО  
                        }
                        else MainWorkSheet.Cells[CurrentButtomPosition, 6] = "-";

                        if (item.Contractor.PasportSeries != null && item.Contractor.PasportNum != null)
                        {
                            MainWorkSheet.Cells[CurrentButtomPosition, 7] = item.Contractor.PasportSeries +
                                                                            " " + item.Contractor.PasportNum; //серия и номер пасопрта      
                        }
                        else MainWorkSheet.Cells[CurrentButtomPosition, 7] = "-";
                        
                        MainWorkSheet.Cells[CurrentButtomPosition, 8] = item.Num + " от " +
                                                                        item.Approvedat.GetValueOrDefault().
                                                                            ToShortDateString();        //№ и дата договора (подписания)

                        MainWorkSheet.Cells[CurrentButtomPosition, 9] = item.Subject ?? "-";            //предмет договора
                      //  MainWorkSheet.Cells[CurrentButtomPosition, 10] = item.Price.ToString() ?? "-";  //цена

                        if (item.Price.HasValue)
                        {
                            MainWorkSheet.Cells[CurrentButtomPosition, 10] = item.Currencymeasure.Factor != null
                                ? NormalPrice(item.Price.Value,item.Currencymeasure.Factor.Value)
                                : (item.Price.Value).ToString(NumberFormat);

                        }
                        else MainWorkSheet.Cells[CurrentButtomPosition, 10] =  "-";
                        

                        //срок действия (от даты принятия договора до окончания работ по нему)
                        MainWorkSheet.Cells[CurrentButtomPosition, 11] = item.Appliedat.GetValueOrDefault().ToShortDateString()
                            + " - " + item.Endsat.GetValueOrDefault().ToShortDateString();              
                        
                        //иные условия
                        MainWorkSheet.Cells[CurrentButtomPosition, 12] = item.Description ?? "-";
                       
                        //бенефициары
                        ++CurrentButtomPosition;
                        ++i;

                        //MainWorkSheet.Range["A" + CurrentButtomPosition.ToString(),
                        //    "T" + CurrentButtomPosition.ToString()].Borders.Weight = xl
                    }

                    MainWorkSheet.Rows.AutoFit();
                }
        }
    }
}
